
const MessageType = {}

MessageType.MEMBER_LOGIN = 1  // 普通账号 注册
MessageType.MEMBER_LOGOUT = 2  // 普通账号 注销
MessageType.MEMBER_CREATE_ORDER = 3 // 普通账号 下单

MessageType.RECYCLER_LOGIN = 5  // 回收员 注册
MessageType.RECYCLER_LOGOUT = 6  // 回收员 注销
MessageType.RECYCLER_BIND_ORDER = 7  // 回收员 接单
MessageType.RECYCLER_STAFF_POSITION = 8 // 员工上传定位

MessageType.AGENT_LOGIN = 9  // 代理商 注册
MessageType.AGENT_LOGOUT = 10  // 代理商 注销
MessageType.AGENT_BIND_ORDER = 11  // 代理商 接单
MessageType.AGENT_LISTEN_STAFF_POSITION = 12 // 员工定位
MessageType.AGENT_LISTEN_STAFF_LIST = 14 // 代理商监听员工的在线状态

MessageType.LISTEN_ORDER_LIST = 13 // 订单列表

MessageType.CHANGE_ONLINE = 15 // 改变在线状态 离线状态不推单


MessageType.REMOVE_ORDER = 16 // 删除订单

const GeneratorMessage = function(type, data) {
	let message = {};
	message.message = data;
	message.type = type;
	message.send_time = (new Date()).getTime();
	return JSON.stringify(message);
}

const WebSocket = class {
	constructor() {
		this.host;
		this.port;
		this.ws;
		this.service_list = [];
		this.debug = true;
		this.debug = false;
		this.on_change_service_list = null;
		this.say_coro = [{
			'name': '',
			'action': () => {}
		}];
		this.is_sync_world_position = false; // 控制开关
		this.sysn_world_posoition = {};
		this.sync_world_action = null;
		this.onOrderListChange = null;
		this.onAgentListenSatffPosition = null;
		this.onAgentListenStaffList = null;
		
	}
	
	create(host, port) {
		let self = this;
		this.ws = uni.connectSocket({
			url: "wss://" + host + ":" + port,
			success(data) {
				// console.log(data)
			},
		});
		this.ws.onMessage = uni.onSocketMessage(function(coro) {
			self.on_message(coro)
		})
	}
	
	on_message(res) {
		let data = JSON.parse(res.data);
		let type = data['type'];
		let msg = data['message'];
		// this.debug && console.log(res)
		// console.log('监听服务器返回', res)
		// this.agentListenStaffPosition(data)
		// this.agentListenStaffOnline(data)
		this.listenList(data)
	}
	
	// 删除订单
	removeOrder(order_id) {
		let data = {
			order_id: order_id
		}
		this.ws.send({
			data: GeneratorMessage(MessageType.REMOVE_ORDER, data),
			async success(coro) {
				console.log("删除订单WS！");
				console.log("成功...");
				callback && callback(coro)
			}
		});
	}
	
	// 普通账号注册
	userLoginIn(user_id, user_name, user_mobile, agent_id, callback) {
		let data = {
			user_id: user_id,
			user_name: user_name,
			user_mobile: user_mobile,
			agent_id: agent_id
		}
		this.ws.send({
			data: GeneratorMessage(MessageType.MEMBER_LOGIN, data),
			async success(coro) {
				console.log("小程序用户登录WS！");
				console.log("成功...");
				callback && callback(coro)
			}
		});
	}
	// 普通账号注销
	userLoginOUT(user_id) {
		let data = {
			user_id: user_id
		}
		this.ws.send({
			data: GeneratorMessage(MessageType.MEMBER_LOGOUT, data),
			async success(coro) {
				console.log("小程序用户注销WS！");
				console.log("成功...");
				callback && callback(coro)
			}
		});
	}
	// 普通账号下单
	userCreateOrder(user_id,id, create_time, address,detail_address,realname,goods_category_name,weight,latitude,longitude, status,agent_id) {
		let data = {
			user_id: user_id,
			id: id,
			create_time: create_time,
			address: address,
			detail_address: detail_address,
			realname: realname,
			goods_category_name: goods_category_name,
			weight: weight,
			latitude: latitude,
			longitude: longitude,
			status: status,
			agent_id: agent_id
		}
		this.ws.send({
			data: GeneratorMessage(MessageType.MEMBER_CREATE_ORDER, data),
			async success(coro) {
				console.log('推送成功，data:', data)
			}
		});
	}
	
	// 回收员 注册
	recylerLogin(user_id, user_name, user_mobile, agent_id, online, callback) {
		let data = {
			user_id: user_id,
			user_name: user_name,
			user_mobile: user_mobile,
			agent_id: agent_id,
			online: online
		}
		this.ws.send({
			data: GeneratorMessage(MessageType.RECYCLER_LOGIN, data),
			async success(coro) {
				console.log("回收员登陆成功！");
				console.log("您已处在在线状态...");
				callback && callback(coro)
			}
		});
	}
	// 回收员注销
	recyclerLoginOut(user_id) {
		let data = {
			user_id: user_id
		}
		this.ws.send({
			data: GeneratorMessage(MessageType.RECYCLER_LOGOUT, data),
			async success(coro) {
				
			}
		});
	}
	// 回收员接单
	recyclerGetOrder(recyler_user_id, order_id) {
		let data = {
			recyler_user_id: recyler_user_id,
			order_id: order_id
		}
		this.ws.send({
			data: GeneratorMessage(MessageType.RECYCLER_BIND_ORDER, data),
			async success(coro) {
				console.log('接单')
			}
		});
	}
	// 回收员上传定位
	staffPosition(user_id, user_name, user_mobile, latitude, longitude, agent_id, callback) {
		let data = {
			user_id: user_id,
			user_name: user_name,
			user_mobile: user_mobile,
			latitude: latitude,
			longitude: longitude,
			agent_id: agent_id
		}
		this.ws.send({
			data: GeneratorMessage(MessageType.RECYCLER_STAFF_POSITION, data),
			async success(coro) {
				console.log("上传定位信息！");
				console.log("更新定位成功...");
				callback && callback(coro)
			}
		});
	}

	// // 代理商 注册
	agentLogin(user_id, user_name, user_mobile, online, callback) {
		let data = {
			user_id: user_id,
			user_name: user_name,
			user_mobile: user_mobile,
			online: online
		}
		this.ws.send({
			data: GeneratorMessage(MessageType.AGENT_LOGIN, data),
			async success(coro) {
				console.log("代理商登陆成功！");
				console.log("您已处在在线状态...");
				callback && callback(coro)
			}
		});
	}
	// 代理商 注销
	agentLoginOut(user_id) {
		let data = {
			user_id: user_id
		}
		this.ws.send({
			data: GeneratorMessage(MessageType.AGENT_LOGOUT, data),
			async success(coro) {

			}
		});
	}
	// 代理商 接单
	agentGetOrder(agent_user_id, order_id) {
		let data = {
			agent_user_id: agent_user_id,
			order_id: order_id
		}
		this.ws.send({
			data: GeneratorMessage(MessageType.AGENT_BIND_ORDER, data),
			async success(coro) {
		
			}
		});
	}
	// // 代理商 查看员工定位
	// agentListenStaffPosition(callback) {
	// 	this.ws.send({
	// 		data: GeneratorMessage(MessageType.AGENT_LISTEN_STAFF_POSITION, {}),
	// 		async success(coro) {
	// 			callback(coro)
	// 		}
	// 	});
	// }
	// // 代理商 监听员工状态
	// agentListenStaffOnline(callback) {
	// 	this.ws.send({
	// 		data: GeneratorMessage(MessageType.AGENT_LISTEN_STAFF_LIST, {}),
	// 		async success(coro) {
	// 			callback(coro)
	// 		}
	// 	});
	// }
	
	// 订单监听
	listenList(res){
		// console.log(JSON.parse())
		let type = res.type
		let msg = res.message
		
		if (type == MessageType.LISTEN_ORDER_LIST) {
			this.onOrderListChange && this.onOrderListChange(msg)
		}
		if (type == MessageType.AGENT_LISTEN_STAFF_POSITION) {
			this.onAgentListenSatffPosition && this.onAgentListenSatffPosition(msg)
		}
		if (type == MessageType.AGENT_LISTEN_STAFF_LIST) {
			this.onAgentListenStaffList && this.onAgentListenStaffList(msg)
		}
		// this.ws.send({
		// 	data: GeneratorMessage(MessageType.LISTEN_ORDER_LIST, {}),
		// 	async success(coro) {
		// 		callback(coro)
		// 	}
		// });
	}
	
	// 改变在线状态
	userChangeOnline(user_id, online, callback) {
		let data = {
			user_id: user_id,
			online: online
		}
		this.ws.send({
			data: GeneratorMessage(MessageType.CHANGE_ONLINE, data),
			async success(coro) {
				callback && callback(coro)
			}
		});
	}
	
	
	
	
	

}

export {
	WebSocket,
	MessageType,
	GeneratorMessage
}
